﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Chapter07.View
{
    using Chapter07.ViewModel;
    using System.ComponentModel;
    using System.Collections.Specialized;
    using System.Collections.ObjectModel;

    /// <summary> 
    /// DocumentWindow.xaml 的交互逻辑
    /// </summary>
    public partial class TrackerWindow : Window
    {
        private bool isSave = true;
        private ObservableCollection<Document> docment = new ObservableCollection<Document>();

        public TrackerWindow()
        {
            InitializeComponent();
        }

        private void AddDocument(string path)
        {
            FileViewerWindow view;
            if (path != null)
                view = new FileViewerWindow(path);
            else
                view = new FileViewerWindow { Title = "空文档" };
            var doc = new Document(view.Title, view);
            doc.Closd += (sender, e) => docment.Remove(e.Docment);
            view.Owner = this;
            view.Show();
            docment.Add(doc);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            isSave = true;
            foreach (var path in ((App)Application.Current).CommandLine)
            {
                AddDocument(path);
            }
            lst.DataContext = docment;
        }

        private void Window_Closing(object sender, CancelEventArgs e)
        {
            if (!isSave)
            {
                e.Cancel = true;
                MessageBox.Show("你有未保存的数据.");
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            AddDocument(null);
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            foreach (var doc in docment)
            {
                doc.FileView.SetContent("已更新于 " + DateTime.Now.ToLongTimeString() + ".");
            }
        }
    }
}
